//
//  DZWebViewController.m
//  Meidaojia
//
//  Created by Darren on 15/5/29.
//  Copyright (c) 2015年 Darren Zheng. All rights reserved.
//

#import "DZWebViewController.h"



@interface DZWebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webViewMain;
@end

@implementation DZWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initnavegation];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self.view addSubview:self.webViewMain];
    [self.webViewMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
    [self.webViewMain loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.strURL]]];
}

#pragma mark - UIWebViewDelegate Methods

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"%@", request.URL);
    NSString *requestString = [NSString stringWithFormat:@"%@", request.URL];
    NSString *string = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *protocol = @"app-call://";
    if ([string hasPrefix:protocol]) {
        NSArray *arr = [string componentsSeparatedByString:@"//"];
        if (arr.count > 1) {
//            NSDictionary *dic = dictionaryWithJsonString(arr[1]);
//            if ([dic[@"action"] isEqualToString:@"order"]) {
//                if (UserDefaults.mid.length < 1 && UserDefaults.password.length < 1) {
//                    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[MLoginVC new]] animated:YES completion:nil];
//                    return NO;
//                }
//                MOrderVC *vc = [[MOrderVC alloc] init];
//                vc.activityid = [NSString stringWithFormat:@"%@", dic[@"activityid"]];
//                vc.pid = [NSString stringWithFormat:@"%@", dic[@"pid"]];
//                vc.fid = [NSString stringWithFormat:@"%@", dic[@"fid"]];
//                vc.service_type = [NSString stringWithFormat:@"%@", dic[@"service_type"]];
//                vc.address = [NSString stringWithFormat:@"%@", dic[@"service_address"]];
//                [self.navigationController pushViewController:vc animated:YES];
//                
//                return NO;
//            }
        }
    }
    return YES;
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
//    ProgressHUDStr;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
//    ProgressDismiss;
}
- (UIWebView *)webViewMain
{
    if (!_webViewMain)
    {
        _webViewMain = [UIWebView new];
        _webViewMain.delegate = self;
    }
    return _webViewMain;
}



@end
